package com.example.traffic_learning;

import global.AsyncResponse;

import java.util.ArrayList;

import DTO.TrafficLawDTO;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;


public class GetDatabaseTask extends AsyncTask<String, Integer, String>{
	Context context;
	public AsyncResponse delegate=null;
	private ProgressDialog progressDialog;
	private boolean isFirst = true;
	public GetDatabaseTask(Context context) {
		this.context = context;
	}

	@Override
	protected String doInBackground(String... params) {

			String result = "";
			ArrayList<TrafficLawDTO> arrTrafficLawDTO = new ArrayList<TrafficLawDTO>();
			try{
				arrTrafficLawDTO = TrafficLawDTO.getByKeyWord(context, params[0], FragLawTrafficLearning.chapter);
				result = law(arrTrafficLawDTO);
			}catch(Exception e){
				Log.e("ABCD", e.toString());
			}
			if(this.isCancelled()){
				progressDialog.dismiss();
			}
			return result;
			
	}
	
	@Override
	protected void onPreExecute(){
		super.onPreExecute();
		if(isFirst()){
			progressDialog = new ProgressDialog(context);
			progressDialog.setTitle("Đang tải dữ liệu....");
			progressDialog.setMessage("Vui lòng đợi!");
			progressDialog.setCancelable(false);
			progressDialog.setIndeterminate(true);
			progressDialog.show();
		}
	}
	
	@Override
	protected void onPostExecute(String result){
		super.onPostExecute(result);
		if(progressDialog != null){
			progressDialog.dismiss();
		}
		delegate.processGetDatabaseFinish(result);
	}
	
	private String law(ArrayList<TrafficLawDTO> listTrafficLawDTO){
		String result = "";
		ArrayList<String> listChapterName = new ArrayList<String>();
		ArrayList<String> listChapter = new ArrayList<String>();
		ArrayList<String> listArticleName = new ArrayList<String>();
		ArrayList<String> listArticle = new ArrayList<String>();
		for(int i = 0; i < listTrafficLawDTO.size(); i++){
			if(!listArticleName.contains(listTrafficLawDTO.get(i).getArticleName())){
				listArticleName.add(listTrafficLawDTO.get(i).getArticleName());
				listArticle.add(listTrafficLawDTO.get(i).getArticle());
			}
		}
		
		for(int i = 0; i < listTrafficLawDTO.size(); i++){
			if(!listChapterName.contains(listTrafficLawDTO.get(i).getChapterName())){
				listChapterName.add(listTrafficLawDTO.get(i).getChapterName());
				listChapter.add(listTrafficLawDTO.get(i).getChapter());
			}
		}
		String strChapter = listChapterName.get(0);
		String strArticle = listArticleName.get(0);
		int i = 0;
		int j = 0;
		result += "<strong><p>Chương " + listChapter.get(i) + ": " + strChapter +"</p></strong>";
		result += "<strong><p>Điều " + listArticle.get(j) + ": " + strArticle +"</p></strong>";
		
		for(int t = 0; t < listTrafficLawDTO.size(); t++){	
			if(listChapterName.contains(listTrafficLawDTO.get(t).getChapterName())){
				if(strChapter.equals(listTrafficLawDTO.get(t).getChapterName())){
					if(listArticleName.contains(listTrafficLawDTO.get(t).getArticleName())){
						if(strArticle.equals(listTrafficLawDTO.get(t).getArticleName())){
							result += listTrafficLawDTO.get(t).getContents();
						}else{
							j++;
							strArticle = listTrafficLawDTO.get(t).getArticleName();
							result += "<strong><p>Điều " + listArticle.get(j) + ": " + strArticle +"</p></strong>";
							t--;
						}
					}
				}else{
					i++;
					strChapter = listTrafficLawDTO.get(t).getChapterName();
					result += "<strong><p>Chương " + listChapter.get(i) + ": " + strChapter +"</p></strong>";
					t--;
				}
			}
			
		}
		return result;
	}

	public boolean isFirst() {
		return isFirst;
	}

	public void setFirst(boolean isFirst) {
		this.isFirst = isFirst;
	}
}
